﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Live;
using Common.Library;
using System.Threading.Tasks;

namespace SyncStories
{
    public partial class SyncButton : UserControl
    {       
        public static LiveConnectSession session = default(LiveConnectSession);
        public SyncButton()
        {
            InitializeComponent();
            //Loaded += SyncButton_Loaded;
        }
        
        public static async Task Login()
        {
            //bool connected1 = false;
            //if(!string.IsNullOrEmpty(AppSettings.MicrosoftAccessToken))
            //{
            //    connected1=true;
            //    AppSettings.SkyDriveLogin = true;
            //}
           // else
            //{
                bool connected1 = false;
                try
                {
                var authclient1 = new LiveAuthClient();
                LiveLoginResult res = await authclient1.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.basic", "wl.offline_access", "wl.skydrive_update", "wl.emails" });
                    
                    if(res.Status==LiveConnectSessionStatus.Connected)
                    {
                    connected1 = true;
                    var connectClient1 = new LiveConnectClient(res.Session);
                    string AccessTkn=connectClient1.Session.AccessToken;
                    session = connectClient1.Session;
                    AppSettings.MicrosoftAccessToken = AccessTkn;
                    AppSettings.SkyDriveLogin = true;
                    var meresult = await connectClient1.GetAsync("me");
                    dynamic meData = meresult.Result;
                    }
                }
                 catch (LiveAuthException ex) { MessageBox.Show(ex.Message); }
                 catch (LiveConnectException ex) { MessageBox.Show(ex.Message); }
            
               //btnSignin.Visibility = connected1 ? Visibility.Collapsed : Visibility.Visible;            
        }

        //private async void btnSignin_Click(object sender, RoutedEventArgs e)
        //{
        //    bool connected=false;
        //    try
        //    {
        //        var authClient = new LiveAuthClient();
        //        LiveLoginResult result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.basic", "wl.offline_access", "wl.skydrive_update" });

        //        if (result.Status == LiveConnectSessionStatus.Connected)
        //        {
        //            connected = true;                    
        //            var connectClient = new LiveConnectClient(result.Session);
        //            AccessTkn=connectClient.Session.AccessToken;
        //            AppSettings.MicrosoftAccessToken = AccessTkn;
        //            var meResult = await connectClient.GetAsync("me");
        //            dynamic meData = meResult.Result;
        //            //updateUI(meData);
        //        }
        //    }
        //    catch (LiveAuthException ex)
        //    {
        //        MessageBox.Show(ex.Message);// Display an error message.
        //    }
        //    catch (LiveConnectException ex)
        //    {
        //        MessageBox.Show(ex.Message);  // Display an error message.
        //    }

            // Turn off the display of the connection button in the UI.
            //btnSignin.Visibility = connected ? Visibility.Collapsed : Visibility.Visible;
            //if (connected == true)
            //{
            //   btnSignin.IsEnabled = false;
            //    AppSettings.SkyDriveLogin = true;
            //}        
    }
}
